#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import sys
import random

import PyQt5

from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QVBoxLayout, QHBoxLayout, QGridLayout, QWidget, QPushButton, QLCDNumber
from PyQt5.QtGui import QIcon

class LCDCounter(QLCDNumber):
    def __init__(self):
        super().__init__()
        self.setSegmentStyle(QLCDNumber.Flat)
        self.setStyleSheet("background: black; color: red")

class BombBtn(QPushButton):
    def __init__(self, row, col, isBomb):
        super().__init__()

        self.mRow = row
        self.mCol = col
        self.mIsBomb = isBomb
        self.mNumber = 0
        self.setFixedSize(25, 25)

        if self.mIsBomb:
            # btn显示图片
            icon = QIcon("res/mine.jpg")
            self.setIcon(icon)

    def GetPos(self):
        return (self.mRow, self.mCol)

    def AddNumber(self):
        self.mNumber += 1
        if not self.mIsBomb:
            self.setText(str(self.mNumber))

    def IsBomb(self):
        return self.mIsBomb

    ## 按钮上的鼠标按下事件
    def mousePressEvent(self, e):
        #print("%s:" % self.mText, end = '')
        if Qt.LeftButton == e.buttons():
            print("left pressed")
        elif Qt.RightButton == e.buttons():
            print("right pressed")
        else:
            print("e.btns == %d" % e.buttons())

class BombsMap():
    def __init__(self, row, col, rate):
        self.mRow = row
        self.mCol = col
        self.mRate = rate
        self.mLayout = QGridLayout()

        self.mBombsNumber = 0
        self.mBtnsNumber = self.mRow * self.mCol

        self.mBtnsMap = {}

        # 布局雷区
        for r in range(0, self.mRow):
            for c in range(0, self.mCol):
                isBomb = self.RateTrue(self.mRate)
                if isBomb:
                    self.mBombsNumber += 1

                btn = BombBtn(r, c, isBomb)
                self.mLayout.addWidget(btn, r, c)

                pos = (r, c)
                self.mBtnsMap[pos] = btn

        # 生成数字
        for pos in self.mBtnsMap:
            btn = self.mBtnsMap[pos]
            print(pos, end = '')
            if btn.IsBomb():
                pos = btn.GetPos()
                print(":isBomb(%s): " % str(pos), end = '')
                self.AddNeighborNumber(pos)
            else:
                print()

    def AddNeighborNumber(self, pos):
        posList = self.GetNeighborList(pos)
        print(posList)
        for pos in posList:
            btn = self.mBtnsMap[pos]
            btn.AddNumber()

    def GetNeighborList(self, pos):
        posList = []
        (row, col) = pos
        # 所有9个相邻的位置
        for r in range(row - 1, row + 2):
            for c in range(col - 1, col + 2):
                # 排除 r 越界
                if r < 0 or r >= self.mRow:
                    continue
                # 排除 c 越界
                if c < 0 or c >= self.mCol:
                    continue
                # 排除 自己
                if r == row and c == col:
                    continue

                pos = (r, c)
                posList.append(pos)

        return posList

    def RateTrue(self, rate):
        if (rate > 1.0) or (rate < 0.0):
            print("rate must be [0,1]")
        rand = random.random()

        if rand < rate:
            return True
        else:
            return False

    def GetBombsNumber(self):
        return self.mBombsNumber

    def GetBtnsNumber(self):
        return self.mBtnsNumber

    def Layout(self):
        return self.mLayout

class MineWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.mBombsMap = BombsMap(20, 30, 0.25)
        self.InitGui()

    def InitGui(self):
        """
        w_width = 304
        w_height = 344
        self.resize(w_width, w_height)
        self.setFixedSize(self.width(), self.height())
        """
        self.setWindowTitle("扫雷")

        allLayout     = QVBoxLayout()
        headLayout    = QHBoxLayout()
        bombsLcdNumber = LCDCounter()
        btnRst = QPushButton()
        btnRst.setFixedSize(80,50)
        btnRst.setText("复位")
        btnsLcdNumber  = LCDCounter()

        headLayout.addWidget(bombsLcdNumber)
        headLayout.addWidget(btnRst)
        headLayout.addWidget(btnsLcdNumber)

        bombsLcdNumber.display(str(self.mBombsMap.GetBombsNumber()))
        btnsLcdNumber.display(str(self.mBombsMap.GetBtnsNumber()))

        allLayout.addLayout(headLayout)
        allLayout.addLayout(self.mBombsMap.Layout())

        #self.setFixedSize(500, 500)
        self.setLayout(allLayout)

if __name__ == '__main__':
    app = QApplication(sys.argv)

    w = MineWindow()
    w.show()

    rtn = app.exec_()
    sys.exit(rtn)

